package cc.linker.steplink.crm.task;

import cc.linker.steplink.crm.commons.lock.RedisLock;
import cc.linker.steplink.crm.dao.ArticleRuleDao;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 文章标签服务
 *
 * @author wuyue
 */
@Slf4j
@RestController
public class ArticleLabelService {

    private ArticleRuleDao articleRuleDao;

    @Autowired
    public ArticleLabelService(ArticleRuleDao articleRuleDao) {
        this.articleRuleDao = articleRuleDao;
    }

    /**
     * 手动调用
     */
    @GetMapping("/autoArticleLabelToDo")
    @ResponseBody
    public String updateAccountMoneyDayTasksToDo(){
        service();
        return "ok";
    }

    @RedisLock("ArticleLabelService")
    @Scheduled(cron = "0 0 0 * * ?")
    public void service() {
        // 打标签规则
        List<Map<String, Object>> list = articleRuleDao.listAll();
        for (Map<String, Object> map : list) {
            String orgId = (String) map.get("orgId");
            Integer condition = (Integer) map.get("condition");
            if (!StringUtils.isEmpty(orgId) && condition != null) {
                // 根据规则和机构ID得到客户-标签名
                List<Map<String, Object>> articleLabels;
                if (condition == 1) {// 满足一个
                    articleLabels = articleRuleDao.getArticleLabelsSelective(map);
                } else if (condition == 2) {// 满足所有
                    articleLabels = articleRuleDao.getArticleLabels(map);
                } else {
                    LOGGER.info("condition不匹配");
                    continue;
                }
                for (Map<String, Object> articleLabel : articleLabels) {
                    String labels = (String) articleLabel.get("articleLabels");
                    if (StringUtils.isEmpty(labels)) {
                        continue;
                    }
                    String customerId = (String) articleLabel.get("customerId");
                    if (StringUtils.isEmpty(customerId)) {
                        LOGGER.info("客户ID空");
                        continue;
                    }
                    // 根据标签名查找标签id
                    Map<String, Object> selectMap = new HashMap<>();
                    selectMap.put("labels", labels);
                    selectMap.put("orgId", orgId);
                    List<String> labelIds = articleRuleDao.getLabelIds(selectMap);
                    if (labelIds != null && labelIds.size() > 0) {
                        Map<String, Object> insertMap = new HashMap<>();
                        insertMap.put("customerId", customerId);
                        insertMap.put("labelIds", labelIds);
                        articleRuleDao.insertToLabel(insertMap);
                    }
                }
            }
        }
    }

}
